<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="申报年度" name="year">
            <t-date-picker style="width: 240px" v-model="queryParams.year" mode="year" clearable/>
          </t-form-item>
          <t-form-item label="成果名称" name="innovationName">
            <t-input
              v-model="queryParams.innovationName"
              clearable
              placeholder="请输入成果名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="负责人" name="leaderName">
            <t-input
              v-model="queryParams.leaderName"
              clearable
              placeholder="请输入负责人"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="申报单位" name="deptName">
            <t-input
              v-model="queryParams.deptName"
              clearable
              placeholder="请输入申报单位"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="应用领域" name="applicationField">
            <t-select style="width: 240px" clearable v-model="queryParams.applicationField"
                      :options="dict.type.application_field">
            </t-select>
          </t-form-item>
          <t-form-item label="创新类别" name="innovationCategory">
            <t-select style="width: 240px" clearable v-model="queryParams.innovationCategory"
                      :options="dict.type.innovation_category">
            </t-select>
          </t-form-item>
          <t-form-item label="归口单位" name="managerIdList">
            <t-select multiple @change="getList" style="width: 300px" :keys="{label:'deptName', value: 'id'}" :options="deptManagerOptions" clearable filterable v-model="queryParams.managerIdList"></t-select>
          </t-form-item>
                    <t-form-item label="成果状态" name="innovationStatus">
                      <t-select  style="width: 240px" clearable v-model="queryParams.innovationStatus"
                                 :options="dict.type.innovation_status">
                      </t-select>
                    </t-form-item>
<!--          <t-form-item label="审批状态" name="approveStatus">-->
<!--            <t-select style="width: 240px" clearable v-model="queryParams.approveStatus"-->
<!--                      :options="dict.type.approve_status">-->
<!--            </t-select>-->
<!--          </t-form-item>-->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="exportReviewExcel" variant="outline" theme="primary">
        <t-icon name="download" slot="icon"/>
        导出评审信息
      </t-button>
    </div>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :sort="sort"
      :hideSortTips="true"
      @sort-change="sortChange"
    >
      <template #[`innovations.name`]="{ row }">
        <t-link @click="dialog.objId=row.innovations.id;dialog.header=row.innovations.name;dialog.open_innovation_detail=true" hover="color" theme="primary">{{row.innovations.name}}</t-link>
      </template>
      <template #reviewTeamName="{ row }">
        <t-link @click="dialog.objId=row.teamId;dialog.header='评审组详情';dialog.open_review_group=true" hover="color" theme="primary">{{ row.reviewTeamName }}</t-link>
      </template>
      <template #[`innovations.applicationField`]="{row}">
        {{ row.innovations.applicationField | filterByDict(dict.type.application_field) }}
      </template>
      <template #[`innovations.innovationCategory`]="{row}">
        {{ row.innovations.innovationCategory | filterByDict(dict.type.innovation_category) }}
      </template>
      <template #[`innovations.innovationStatus`]="{ row }">
        <t-tag  @click="dialog.header='审批记录';dialog.objId=row.innovations.id;dialog.open_approve_list=true" class="cursor-pointer" :theme="handleTheme(row.innovations.innovationStatus, dict.type.innovation_status)" variant="light">
          {{ row.innovations.innovationStatus | filterByDict(dict.type.innovation_status) }}
        </t-tag>
      </template>
      <template #[`innovationAwards.awardsLevel`]="{ row }">
        {{ row.innovationAwards?.awardsLevel | filterByDict(dict.type.wcx_awards_level) }}
      </template>
<!--      innovationAwards.awardsLevel-->
<!--      <template #isReviewed="{row}">-->
<!--        <t-tag theme="success" v-if="row.isReviewed==='1'">-->
<!--          是-->
<!--        </t-tag>-->
<!--        <t-tag theme="danger" v-else-if="row.isReviewed==='0'">-->
<!--          否-->
<!--        </t-tag>-->
<!--        <t-tag theme="warning" v-else>-->
<!--          未提交-->
<!--        </t-tag>-->
<!--      </template>-->

      <template #op="{ row }">
<!--        <t-button  v-hasPermi="['*:*:*']" @click="updateData(row)" size="small" variant="text"-->
<!--                    class="t-button-link">-->
<!--          <t-icon name="edit-1" slot="icon"/>-->
<!--          编辑-->
<!--        </t-button>-->
        <t-button @click="dialog.header=row.innovations.name;dialog.objId=row.innovations.id;dialog.open_innovation_detail=true" size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
        <t-button @click="createPDF(row)" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon"/>
          导出PDF
        </t-button>
        <t-button @click="createZip(row)" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="folder-zip" slot="icon"/>
          导出附件
        </t-button>
      </template>
      <template #opReview="{ row }">
        <t-button  @click="dialog.objId=row.id;dialog.reviewType = row.reviewType;dialog.projectName=row.innovations.name;dialog.innovationId=row.innovations.id;dialog.open_project_approval=true;"  size="small" variant="text"
                   class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          评奖
        </t-button>
        <t-button    @click="dialog.objId=row.innovations.id;dialog.reviewType = row.reviewType;dialog.teamId=row.teamId;dialog.projectName=row.innovations.name;dialog.open_review_list=true" size="small" variant="text"
                   class="t-button-link">
          <t-icon name="root-list" slot="icon"/>
          专家评审详情
        </t-button>

        <t-button @click="dialog.header=row.innovations.name;dialog.objId=row.innovations.id;dialog.editOpen=false;dialog.open_detail=true" size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          推荐详情
        </t-button>
      </template>
    </t-table>
    <innovation-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
    <review-group-all-detail :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_review_group" v-if="dialog.open_review_group"></review-group-all-detail>
    <expert-review-list :teamId="dialog.teamId" :reviewType="dialog.reviewType" :objId="dialog.objId" :projectName="dialog.projectName" :visible.sync="dialog.open_review_list"
                        v-if="dialog.open_review_list"></expert-review-list>
    <recommend-detail-dialog :editOpen="dialog.editOpen" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></recommend-detail-dialog>
    <innovation-award-approval @reload="getList" :reviewType="dialog.reviewType" :header="dialog.header" :projectName="dialog.projectName" :innovationId="dialog.innovationId" :objId="dialog.objId" :visible.sync="dialog.open_project_approval" v-if="dialog.open_project_approval"></innovation-award-approval>

  </div>
</template>

<script>
import InnovationDetailDialog from "../innovation/components/innovation-detail-dialog.vue";
import ApproveList from "../approve/components/approve-list.vue";
import ReviewGroupAllDetail from "./components/review-group-all-detail.vue";
import dayjs from "dayjs"
import RecommendDetailDialog from "../recommend/components/recommend-detail-dialog.vue";
import expertReviewList from "./components/expert-review-list.vue";
import innovationAwardApproval from "./components/innovation-award-approval.vue";


export default {
  name: "innovation-index",
  dicts: ["application_field", "innovation_category", "approve_status", "innovation_status", "wcx_awards_level"],
  components:{
    InnovationDetailDialog,
    ApproveList,
    ReviewGroupAllDetail,
    RecommendDetailDialog,
    expertReviewList,
    innovationAwardApproval
  },
  data() {
    return {
      sort: {
        // 按照 status 字段进行排序
        sortBy: 'sumScore',
        // 是否按照降序进行排序
        descending: false,
      },
      deptManagerOptions: [],
      dialog:{
        header:"",
        objId:0,
        open_innovation_detail: false,
        open_approve_list: false,
        open_review_group: false,
        open_review_list: false,
        open_detail: false,
        open_project_approval: false,
        reviewType: ''
      },
      data: [],
      columns: [
        {
          colKey: "innovations.name",
          title: "成果名称",
          align: "center",
          width: 220
        },
        {
          colKey: "reviewTeamName",
          title: "评审组",
          align: "center",
        },
        {
          colKey: "sumScore",
          title: "评审平均分",
          align: "center",
          sorter: true,
          width: 140
        },
        {
          colKey: "innovations.applicationProject",
          title: "应用项目",
          align: "center",
        },
        {
          colKey: "innovations.applicationField",
          title: "应用领域",
          align: "center",
        },
        {
          colKey: "innovations.innovationCategory",
          title: "创新类别",
          align: "center",
        },
        {
          colKey: "innovations.leaderName",
          title: "负责人",
          align: "center",
        },
        {
          colKey: "innovations.deptName",
          title: "工作单位",
          align: "center",
        },
        {
          colKey: "innovationAwards.awardsLevel",
          title: "获奖等级",
          align: "center",
        },
        {
          colKey: "innovations.innovationStatus",
          title: "成果状态",
          align: "center",
          width:120
        },
        // {
        //   colKey: "isReviewed",
        //   title: "是否已进行判定",
        //   align: "center",
        //   width:140
        // },
        // {
        //   colKey: "approveStatus",
        //   title: "审批状态",
        //   align: "center",
        //   width:120
        // },

        {
          colKey: "innovations.createTime",
          title: "申报时间",
          align: "center",
          width: 200
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
        {
          colKey: "opReview",
          title: "评审操作",
          align: "center",
          width: 120
        },
      ],
      dataLoading: false,
      queryParams: {
        year: (dayjs().year() - 1).toString(),
        pageNum: 1,
        pageSize: 10,
        applicationField: '',
        innovationCategory: '',
        innovationName: '',
        innovationStatus: '',
        leaderName: '',
        deptName: '',
        sortType: '1',
        isReviewed: '',
        managerIdList: []
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    Object.assign(this.queryParams,this.handelQuery(this.queryParams,this.$route.query))
    this.getList();
    this.getManagementList();
  },
  methods: {
    exportReviewExcel(){
      this.$download(`/wcx/excel/reviewTeamAllInfo`,this.queryParams, `评审组信息_${new Date().getTime()}.xls`)
    },
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)

        sort.forEach(val=>{
          if(val.sortBy === "sumScore"){
            if(val.descending){
              this.queryParams.sortType = '2'
            }else {
              this.queryParams.sortType = '1'
            }
          }
        })
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
        if(orderByColumn.includes('sumScore')){
          if(sort.descending){
            this.queryParams.sortType = '2'
          }else {
            this.queryParams.sortType = '1'
          }
        }
      }
      if(!orderByColumn.includes('sumScore')){
        this.queryParams.sortType = null
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")

      this.getList();
    },
    getManagementList(){
      this.$api.system.dept.getManagementList().then((res) => {
        this.deptManagerOptions = res.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    createZip(row){
      this.$download(`/wcx/innovations/downLoadZipFile/${row.id}`,{}, `${row.name}.zip`)
    },
    createPDF(row){
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.wcx.apply.innovationPdf(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    updateData(row){
      sessionStorage.setItem(`${row.id}-wcxInnovationId`, row.id);
      this.$router.push(`/wcx/other/apply/innovation-apply/${row.id}`);
    },
    getList() {
      this.dataLoading = true;
      this.$api.wcx.review.innovationList(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    handleExport() {
      this.$download(`/wcx/excel/exportAllList`,this.queryParams, `微创新成果列表_${new Date().getTime()}.xls`)
    }
  }
}
</script>

<style lang="less" scoped>
//.t-col{
//  border: 1px solid #000000;
//  height: 100%;
//  margin-right: -1px;
//  margin-bottom: -1px;
//}
</style>
